package cn.pro.controller;

import cn.pro.api.IProInfoService;
import cn.pro.entity.ProLogInfo;
import cn.pro.entity.qo.Query;
import cn.pro.entity.vo.ProLogInfoVo;
import cn.pro.utils.*;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/log")
public class ProLongInfoController {

    @Autowired
    private IProInfoService iProInfoService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public ProData save(@RequestBody ProLogInfo proLog){
        proLog.setId(ObjectUtils.couponCodeTwo("pro_log"));
        proLog.setCreateTime(new Date());
        proLog.setUpdateTime(new Date());
        CheckMsg msg = iProInfoService.create(proLog);
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "/updata",method = RequestMethod.POST)
    public ProData modify(@RequestBody ProLogInfo proLog){
        CheckMsg msg = iProInfoService.updateLog(proLog);
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "selectList",method = RequestMethod.POST)
    public ProData select(@RequestBody ProLogInfo proLog){
        PageData<ProLogInfoVo> logData = iProInfoService.selectList(proLog);
        if (!org.springframework.util.ObjectUtils.isEmpty(logData)){
            Object obj = JSONArray.toJSON(logData);
            System.out.println("每页个数："+logData.getPageCount()+"  总数"+logData.getTotalCount());
            return ProData.data("200", VableUtil.SUSSES_STATUS,obj.toString());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,"查询异常");
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ProData deleteUser(@RequestBody Query pro){
        CheckMsg msg = iProInfoService.delete(pro.getId());
        if (msg.isSuccess()){
            return ProData.data("200", VableUtil.SUSSES_STATUS,msg.getMesage());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,msg.getMesage());
        }
    }

    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public ProData selectUser(@RequestBody Query pro){
        ProLogInfoVo proLog = iProInfoService.selectById(pro.getId());
        if (!org.springframework.util.ObjectUtils.isEmpty(proLog)){
            return ProData.data("200", VableUtil.SUSSES_STATUS, proLog.toString());
        }else {
            return ProData.data("201", VableUtil.FAIL_STATUS,"查询异常");
        }
    }
}
